/*
 * Includes
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <minix/endpoint.h>
#include <minix/syslib.h>
#include <sys/types.h>
#include <sys/stat.h>

#define NUMBER_OF_STATES	3

enum states {
	STATE_SPEED0, STATE_SPEED250, STATE_ERROR
};

/*
 * Runs the program
 */
int main(int argc, char* argv[]) {
	enum states state;
	enum states nextState[NUMBER_OF_STATES];
	long int cmd;
	char *speed;
	message *m;

	/*if (cmd == 250) state = STATE_SPEED250;
	 else if (cmd == 0) state = STATE_SPEED0;
	 else state = STATE_ERROR;*/

	/*Jeroen*/
	int errno;
	int comEndpoint;
	register message *msgptr;
	{
		int status;
		char test[50] = "s.w.0 s.w.250 STATE_ERROR ;";
		state = STATE_SPEED0;
		msgptr->m_type = INIT_PROC;
		status = _sendrec(PM_PROC_NR, msgptr);
		if (status != 0) {
			printf("Error in sendrec process\n");
		}

		switch (msgptr->m_type) {
		case SEND_ENDPOINT: {
			/*Initialize process*/
			/* get endpoint from com */
			printf("Endpoint from com driver:%d\n", msgptr->RT_DRIVER_ENDPT);
			comEndpoint = msgptr->RT_DRIVER_ENDPT;

		}
			break;
		default: {
			printf("handle process reply: unknown reply type: %d\n",
					msgptr->m_type);
		}
		}

		/*send actions to com driver*/
		msgptr->m_type = INIT_PROC;
		msgptr->RT_DRIVER_ID = getpid();
		msgptr->PROC_ACTIONS = &test;
		status = _sendrec(comEndpoint, msgptr);
		if (status != 0) {
			printf("Error in sendrec process\n");
		}
		switch (msgptr->m_type) {
		case INIT_PROC: {
			printf("Reply received from com driver\n");
		}
			break;
		default: {
			printf("handle process reply: unknown reply type: %d\n",
					msgptr->m_type);
		}
		}

		/*while true met states*/
		msgptr->m_type = ASK_FOR_GO;
		while (1) {
			sleep(5);
			printf("New loop\n");
			msgptr->m_type = ASK_FOR_GO;
			printf("Getpidvalue:%d\n",getpid());
			msgptr->RT_PID = getpid();
				switch (state) {
				case STATE_SPEED0:
				{
					nextState[0] = STATE_SPEED250;
					nextState[1] = STATE_ERROR;
					nextState[2] = STATE_ERROR;
					printf("Try to send state_speed0\n");
					msgptr->PROC_ACTIONS = mapState(nextState[0], state);
					status = _sendrec(comEndpoint, msgptr);
					if (status != 0) {
						printf("Error in sendrec process\n");
					}
					state = nextState[0];
				}
					break;
				case STATE_SPEED250:
				{
					nextState[0] = STATE_SPEED0;
					nextState[1] = STATE_ERROR;
					nextState[2] = STATE_ERROR;
					printf("Try to send state_speed250\n");
					msgptr->PROC_ACTIONS = mapState(nextState[0], state);
					status = _sendrec(comEndpoint, msgptr);
					if (status != 0) {
						printf("Error in sendrec process\n");
					}
					state = nextState[0];
				}
					break;
				default:
				{
					state = STATE_ERROR;
					nextState[0] = STATE_SPEED0;
					nextState[1] = STATE_ERROR;
					nextState[2] = STATE_ERROR;
					msgptr->PROC_ACTIONS = mapState(nextState[0], state);
					status = _sendrec(comEndpoint, msgptr);
					if (status != 0) {
						printf("Error in sendrec process\n");
					}
					state = nextState[0];
				}
					break;
				}
				/*printState(state, nextState);*/
			}
		}
	return (msgptr->m_type);
}

char *mapState(char *action, enum states state) {
	switch (state) {
	case STATE_SPEED0:
		action = "s.w.0";
		break;
	case STATE_SPEED250:
		action = "s.w.250";
		break;
	case STATE_ERROR:
		action = "STATE_ERROR";
		break;
	default:
		printf("No current state found!\n");
		break;
	}
	return action;
}

int printState(enum states state, enum states *nextState) {
	int i;
	char next[80];
	strcpy(next, "Next possible states:\t");

	switch (state) {
	case STATE_SPEED0:
		printf("Current state: STATE_SPEED0\n");
		break;
	case STATE_SPEED250:
		printf("Current state: STATE_SPEED250\n");
		break;
	case STATE_ERROR:
		printf("Current state: STATE_ERROR\n");
		break;
	default:
		printf("No current state found!\n");
		break;
	}
	for (i = 0; i < sizeof(nextState); i++) {
		switch (nextState[i]) {
		case STATE_SPEED0:
			strcat(next, "STATE_SPEED0\t");
			break;
		case STATE_SPEED250:
			strcat(next, "STATE_SPEED250\t");
			break;
		case STATE_ERROR:
			strcat(next, "STATE_ERROR\t");
			break;
		default:
			strcat(next, "No next state found!");
			break;
		}
	}
	strcat(next, "\n");
	printf("%s", next);

	return 0;
}

